package formBean;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.mybeans.form.FormBean;

import util.Util;
import bean.Fund;

public class TransitionDayForm extends FormBean{
	private String dateString;
	private Date date;
	private HashMap<Integer, Long> funds = new HashMap<Integer, Long>();
	private HttpServletRequest request;
	private String button;
	private static SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
	
	public TransitionDayForm(HttpServletRequest request) {
		this.dateString = request.getParameter("dateString");	
		this.button = request.getParameter("button");
		this.request = request;
	}
	
	public boolean isPresent() {
		return !(button == null || button.length() == 0);
	}
	
	public String getDateString() {
		return dateString;
	}
	
	public void setDateString(String dateString) {
		this.dateString = this.trimAndConvert(dateString, "<>\"");
	}
	
	public Date getDate() {
		return date;
	}
	
	public void setDate(Date date) {
		this.date = date;
	}
	
	public ArrayList<String> getValidationErrors(List<Fund> list, Date lastExecuteDate, Date lastOrderDate) {
		ArrayList<String> errors = new ArrayList<String>();
		
		if (dateString == null || dateString.length() == 0) {
			errors.add("Transition date is required.");
		}
		
		if (errors.size() > 0) return errors;
		
		try {
			this.date = dateFormat.parse(dateString);
			
			if (!checkDate(lastExecuteDate, lastOrderDate)) {
				errors.add("Laste execute date: " + lastExecuteDate + ", last order date: " + lastOrderDate);
				errors.add("The date is not right. The execute date should be later than the last execute date, and equal or later than the last order date.");
			}
			
			for (Fund f: list) {
				String priceString = this.trimAndConvert(request.getParameter("" + f.getId()), "<>\"");;
				if (request.getParameter("" + f.getId()) == null) {
					errors.add("Fund " + f.getName() + "'s price is required.");
				} else if (priceString == null || priceString.length() == 0) {
					errors.add("Fund " + f.getName() + "'s price is required.");
				} else {				
					
					if (!Util.isValidMoneyAmount(priceString)) {
						errors.add("Fund " + f.getName() + "'s price should be less than 1,000,000 with only two decimal places.");
					} else {				
						long price = 0;		
						try {
							price = (long) (Double.valueOf(priceString) * 100);				
							if (price <= 0) errors.add("Fund " + f.getName() + "'s price should be greater than 0.");
							else funds.put(f.getId(), price);
						} catch (NumberFormatException e) {
							errors.add("Fund " + f.getName() + "'s price should be a number with only two decimal places.");
						}	
					}
				}
			}
		} catch (ParseException e) {
			e.printStackTrace();
			errors.add("The date formate should be MM/dd/yyyy.");
		}	
		
		
		return errors;
	}
	
	private boolean checkDate(Date lastExecuteDate, Date lastOrderDate) {	
		if (date.getTime() <= 0) return false;
		if (lastExecuteDate == null && lastOrderDate == null) return true;
		if (lastOrderDate == null) return date.after(lastExecuteDate);
		if (lastExecuteDate == null) return !date.before(lastOrderDate);
		
		if (lastExecuteDate.after(lastOrderDate)) return date.after(lastExecuteDate);
		return date.after(lastOrderDate);
	}
	
	public String getPriceString (int id) {
		Long ID = funds.get(id);
		if (ID == null) return null;
		
		return "" + ((double)ID / 100.0);
	}
	
	public long getPrice (int id) {
		return funds.get(id);
	}

	public HashMap<Integer, Long> getFunds() {
		return funds;
	}

	public void setFunds(HashMap<Integer, Long> funds) {
		this.funds = funds;
	}
}
